package com.smile.serverview;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import com.smile.interf.ServerViewInterface;
import com.smile.server.ServerListener;

import java.awt.event.MouseMotionAdapter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainServerView extends JFrame implements ServerViewInterface {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private MainViewPanel contentPane;
	private JTextField portTextField;
	private JLabel portLabel;
	private JButton startButton;
	private JButton minButton;
	private JButton exitButton;
	private JTextArea contentShow;
	private JLabel onlineNumber;

	/**
	 * 鼠标点击时的坐标
	 */
	private int nowX;
	private int nowY;
	/**
	 * 窗口的坐标
	 */
	private int jfX;
	private int jfY;
	/**
	 * 服务器是否开启
	 */
	private boolean isStart = false;
	/**
	 * 
	 */
	private ServerListener serverListener;

	/**
	 * Create the frame.
	 */
	public MainServerView() {

		setUndecorated(true);
		setAlwaysOnTop(true);
		setBackground(new Color(0, 0, 0, 0));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(500, 100, 450, 300);
		setLocation((dim.width - getWidth()) / 2,
				(dim.height - getHeight()) / 2);
		contentPane = new MainViewPanel();
		contentPane.setOpaque(false);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				setLocation(jfX + (e.getXOnScreen() - nowX),
						jfY + (e.getYOnScreen() - nowY));
			}
		});
		panel.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				nowX = e.getXOnScreen();
				nowY = e.getYOnScreen();
				jfX = getX();
				jfY = getY();
			}
		});
		contentPane.add(panel, BorderLayout.NORTH);

		portLabel = new JLabel("端口号:");
		portLabel.setFont(new Font("宋体", Font.PLAIN, 18));

		portTextField = new JTextField();
		portTextField.setMargin(new Insets(0, 5, 0, 0));
		portTextField.setFont(new Font("宋体", Font.PLAIN, 14));
		portTextField.setColumns(1);
		portTextField.setUI(new MyTextField());
		portTextField.setText("8888");

		contentShow = new JTextArea();
		contentShow.setEditable(false);
		contentShow.setLineWrap(true);
		contentShow.setWrapStyleWord(true);
		JScrollPane scroll = new JScrollPane(contentShow);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		contentPane.add(scroll, BorderLayout.CENTER);

		onlineNumber = new JLabel("在线人数:0");
		onlineNumber.setFont(new Font("宋体", Font.PLAIN, 14));
		contentPane.add(onlineNumber, BorderLayout.SOUTH);

		startButton = new JButton("开启服务器端口");
		startButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String portString = portTextField.getText().toString();
				if (!isStart) {
					startPort(portString);
					startButton.setText("关闭服务器端口");
					portTextField.setEditable(false);
				} else {
					serverListener.closeSocket();
					startButton.setText("开启服务器端口");
					portTextField.setEditable(true);
					portTextField.grabFocus();
				}
				isStart = !isStart;
			}
		});
		startButton.setMargin(new Insets(2, 8, 2, 8));
		startButton.setFont(new Font("宋体", Font.PLAIN, 14));
		startButton.setUI(new StartButton());

		minButton = new JButton("一");
		minButton.setMargin(new Insets(0, 0, 0, 0));
		minButton.setFont(new Font("Yu Gothic UI Light", Font.BOLD, 12));
		minButton.setUI(new OvalButton(new Color(70, 145, 250)));
		minButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setExtendedState(JFrame.ICONIFIED);
			}
		});

		exitButton = new JButton("X");
		exitButton.setMargin(new Insets(0, 0, 0, 0));
		exitButton.setFont(new Font("Yu Gothic UI Light", Font.BOLD, 12));
		exitButton.setUI(new OvalButton(Color.RED));
		exitButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(serverListener != null && serverListener.isAlive()){
					serverListener.closeSocket();
				}
				System.exit(0);
			}
		});

		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addGap(4)
						.addComponent(portLabel)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(portTextField,
								GroupLayout.PREFERRED_SIZE, 143,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(startButton)
						.addGap(56)
						.addComponent(minButton, GroupLayout.PREFERRED_SIZE,
								22, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(exitButton, GroupLayout.PREFERRED_SIZE,
								22, GroupLayout.PREFERRED_SIZE)
						.addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGap(0, 0, Short.MAX_VALUE)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(portLabel)
												.addComponent(
														portTextField,
														GroupLayout.PREFERRED_SIZE,
														33,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														startButton,
														GroupLayout.PREFERRED_SIZE,
														33,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(exitButton)
												.addComponent(
														minButton,
														GroupLayout.PREFERRED_SIZE,
														22,
														GroupLayout.PREFERRED_SIZE))
								.addContainerGap()));
		panel.setLayout(gl_panel);
	}

	private void startPort(final String portString) {
		serverListener = new ServerListener(portString, MainServerView.this);
		new SwingWorker<String, String>() {

			@Override
			protected String doInBackground() throws Exception {
				serverListener.start();

				return null;
			}

		}.execute();
	}

	@Override
	public void setMessage(String message, int onlineNumber) {

		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = format.format(date);
		contentShow.append("【" + time + "】" + message);

		this.onlineNumber.setText("在线用户:" + onlineNumber);

		if (message.contains("开启失败")) {
			startButton.setText("开启服务器端口");
			portTextField.setEditable(true);
			portTextField.grabFocus();
			isStart = !isStart;
		}
	}

}
